package model

import (
	"database/sql"
	"github.com/jmoiron/sqlx"
	"time"
	. "zimuzu_cvn_web_api/pkg/e"
	"zimuzu_cvn_web_api/pkg/errno"
)

//返佣-CVNT
func UpdateCvnInviteLogCvnt(db sqlx.Execer, uid int64, cvnt float64) (err error) {
	nowTime := time.Now().Unix()
	res, err := db.Exec(`update cvn_invite_log set cvnt = cvnt + ?, update_time = ? where uid = ?`, cvnt, nowTime, uid)
	if err != nil {
		Log("err", err)
		return err
	}
	row, err := res.RowsAffected()
	if err != nil {
		Log("err", "UpdateCvnInviteLogCvnt:", uid, err)
		return err
	}
	if row != 1 {
		Log("err", "UpdateCvnInviteLogCvnt:", uid, "影响行数为:", row)
		err = errno.UpdateDataErr
		return err
	}
	return nil
}

//返佣-Usdt
func UpdateCvnInviteLogUsdt(db sqlx.Execer, uid int64, usdt float64) (err error) {
	nowTime := time.Now().Unix()
	res, err := db.Exec(`update cvn_invite_log set usdt = usdt + ?, update_time = ? where uid = ?`, usdt, nowTime, uid)
	if err != nil {
		Log("err", err)
		return err
	}
	row, err := res.RowsAffected()
	if err != nil {
		Log("err", "UpdateCvnInviteLogUsdt:", uid, err)
		return err
	}
	if row != 1 {
		Log("err", "UpdateCvnInviteLogUsdt:", uid, "影响行数为:", row)
		err = errno.UpdateDataErr
		return err
	}
	return nil
}

//获取邀请人
func GetInviter(db sqlx.Queryer, uid int64) (inviter int64, err error) {
	err = sqlx.Get(db, &inviter, "SELECT inviter_id FROM `cvn_invite_log` where uid = ?", uid)
	if err == sql.ErrNoRows {
		return 0, nil
	}
	if err != nil {
		Log("err", err)
		return 0, err
	}
	return inviter, nil
}

